home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 12 / CU Amiga Magazine's Super CD-ROM 12 (1997)(EMAP Images)(GB)[!][issue 1997-07].iso / CUCD / Games / DestructivePoker / sources / sources.lha / poker2.cpp < prev    next >
C/C++ Source or Header  |  1997-02-20  |  10KB  |  345 lines

  1. /*
  2.         poker2.cpp      (AmigaOS specific main module)
  3.  
  4.         V1.00 - 031196  Kimmo Teräväinen
  5.         -----   ------  ----------------
  6.         V0.01   031196  Ripped this file from FFeditor.cpp
  7.         V0.20   130297  Edited a lot to suit with poker.
  8.         V0.50   190297  Ready Program if linked with ixemul.
  9.                         But linking with libnix causes GURUs.
  10.         V0.51   200297  Linking with libnix success.
  11. */
  12.  
  13. #include <inline++/exec.h>
  14. #include <inline++/intuition.h>
  15. #include <inline++/dos.h>
  16. #include <inline++/graphics.h>
  17. #include <inline++/gadtools.h>
  18. #include <inline++/asl.h>
  19. #include <inline++/iffparse.h>
  20. #include <inline++/icon.h>
  21. #include <intuition/intuition.h>
  22. #include <intuition/screens.h>
  23. #include <graphics/text.h>
  24. #include <libraries/gadtools.h>
  25. #include <workbench/startup.h>
  26. #include <workbench/workbench.h>
  27.  
  28. #include "cstring.h"
  29.  
  30. #include "poker.h"
  31. #include "game.h"
  32. #include "pokermenu.h"
  33. #include "pokergadgets.h"
  34. #include "card.h"
  35.  
  36. extern WBStartup *_WBenchMsg;
  37.  
  38. extern long LoadBlocks(BitMap &,UBYTE*,UBYTE*);  // load Gfx
  39. extern void FreeBlocks(BitMap &);                // free Gfx
  40. extern void ReMapBitMap(BitMap &,RastPort *,const UBYTE *); // ReMap Gfx
  41. extern int IDCMP(const IntuiMessage *,cIMGCard *const*);
  42.  
  43. UBYTE gfxpens[256];
  44. UBYTE pens[2];
  45.  
  46. UBYTE *PubScreenName=NULL;
  47.  
  48. TextFont *TEXTFONT=NULL;
  49.  
  50.  
  51. int My_main(void)
  52. {
  53.   int RT=20,colors;
  54.   LONG olddir = -1;
  55.   BitMap Cards={0,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
  56.   Screen *Scr=NULL;
  57.   Window *Wnd=NULL;
  58.   ColorMap *cm=NULL;
  59.   UBYTE ColorTable[256*3];
  60.   int   prefsjoker=FALSE,prefs5kind=FALSE,
  61.         changes=1,jokers=3;
  62.   cstring CardFile="gfx/defaultcards.lbm";
  63.  
  64. // Varmista, että levy-kirjastot ovat auki. Jos ei - niin guruta ja poistu
  65.  
  66.   if(!IFFParseBase) {
  67.     UBYTE errortext[] = "\0\100\20iffparse.library\1\1";
  68.     DisplayAlert(0L,errortext,40L);
  69.     return RT;
  70.   }
  71.   if(!GadToolsBase) {
  72.     UBYTE errortext[] = "\0\100\20gadtools.library\1\1";
  73.     DisplayAlert(0L,errortext,40L);
  74.     return RT;
  75.   }
  76.   if(!AslBase) {
  77.     UBYTE errortext[] = "\0\100\20asl.library\1\1";
  78.     DisplayAlert(0L,errortext,40L);
  79.     return RT;
  80.   }
  81.   if(!IconBase) {
  82.     UBYTE errortext[] = "\0\100\20icon.library\1\1";
  83.     DisplayAlert(0L,errortext,40L);
  84.     return RT;
  85.   }
  86.  
  87. #if 1
  88.   if(!_WBenchMsg) {
  89.     Write(Output(),"Start this program from WB\n",28);
  90.     return 10;
  91.   }
  92. #endif
  93.  
  94.  
  95. // Read preferences from TOOLTYPEs
  96.  
  97. //    UBYTE errortext[] = { 0,100,20,'t','e','s','t','i','n','g',0,0 };
  98. //    DisplayAlert(0L,errortext,40L);
  99.  
  100.  
  101.   {
  102.     struct DiskObject *dobj;
  103.     UBYTE **toolarray;
  104.     UBYTE *s;
  105.     WBArg *wbarg=_WBenchMsg->sm_ArgList;
  106.     if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  107.       olddir=CurrentDir(wbarg->wa_Lock);
  108.  
  109.     if((*wbarg->wa_Name) && (dobj=GetDiskObject((UBYTE*)wbarg->wa_Name)))
  110.     {
  111.       toolarray = (UBYTE**)dobj->do_ToolTypes;
  112.  
  113.       if(s=FindToolType(toolarray,(UBYTE*)"PREFSJOKER"))
  114.         if(MatchToolValue(s,(UBYTE*)"TRUE"))
  115.           prefsjoker=TRUE;
  116.  
  117.       if(s=FindToolType(toolarray,(UBYTE*)"PREFS5KIND"))
  118.         if(MatchToolValue(s,(UBYTE*)"TRUE"))
  119.           prefs5kind=TRUE;
  120.  
  121.       if(s=FindToolType(toolarray,(UBYTE*)"CHANGES"))
  122.         changes=s[0]-'0';
  123.       if(changes<1) changes=1;
  124.       if(changes>MAX_CHANGES) changes=MAX_CHANGES;
  125.  
  126.       if(s=FindToolType(toolarray,(UBYTE*)"JOKERS"))
  127.         jokers=s[0]-'0';
  128.       if(jokers<0) jokers=0;
  129.       if(jokers>MAX_JOKERS) jokers=MAX_JOKERS;
  130.  
  131.       if(s=FindToolType(toolarray,(UBYTE*)"CARDS"))
  132.         CardFile=s;
  133.  
  134.       FreeDiskObject(dobj);
  135.     }
  136.  
  137.   }
  138.  
  139. // Opening Font
  140.  
  141.   {
  142.     const TextAttr textattr={ (UBYTE*)"topaz.font",8,0,0 };
  143.     if(!(TEXTFONT=OpenFont(&textattr))) goto cleanup;
  144.   }
  145.  
  146.   {
  147.  
  148. // Load Card graphics, if fail show requester
  149.  
  150.     RT=LoadBlocks(Cards,ColorTable,CardFile);
  151.     if(RT) {
  152.       EasyStruct myES = {
  153.         sizeof(struct EasyStruct),0,
  154.         (UBYTE*)"Destructive Poker",
  155.         (UBYTE*)"Error while loading Card GFX file \"%s\"",
  156.         (UBYTE*)"QUIT"
  157.       };
  158.       EasyRequest(NULL, &myES, NULL, (ULONG)CardFile.chars());
  159.       goto cleanup;
  160.     }
  161.  
  162.  
  163.     if(!(Scr=LockPubScreen(PubScreenName))) goto cleanup;
  164.  
  165.   }
  166.  
  167.  
  168.   {
  169.     {
  170.       int BTop = Scr->WBorTop + (Scr->Font->ta_YSize + 1);
  171.       struct TagItem window_tags[] = {
  172.         { WA_Title, (long unsigned)"Destructive Poker" },
  173.         { WA_DragBar , TRUE},
  174.         { WA_DepthGadget , TRUE},
  175.         { WA_CloseGadget, TRUE },
  176.         { WA_Left, (Scr->Width-600)/2 },
  177.         { WA_Top, (Scr->Height-230-BTop)/2 },
  178.         { WA_Width, 600 }, { WA_Height, 230+BTop },
  179.         { WA_CustomScreen, (ULONG)Scr },
  180.         { WA_Activate, TRUE},
  181.         { WA_IDCMP, IDCMP_VANILLAKEY |
  182.                     IDCMP_MOUSEBUTTONS |
  183.                     IDCMP_MOUSEMOVE |
  184.                     IDCMP_MENUPICK |
  185.                     IDCMP_GADGETUP |
  186.                     IDCMP_REFRESHWINDOW |
  187.                     IDCMP_CLOSEWINDOW },
  188.         { TAG_DONE, NULL}
  189.       };
  190.       RT=22;
  191.       Wnd=OpenWindowTagList(NULL,window_tags);
  192.     }
  193.  
  194.  
  195.     if(Wnd)
  196.     {
  197.       SetFont(Wnd->RPort,TEXTFONT);
  198.  
  199.       Move(Wnd->RPort,8+Wnd->BorderLeft,8+Wnd->BorderTop);    // Print Double
  200.       SetAPen(Wnd->RPort,TEXT_PEN); SetDrMd(Wnd->RPort,JAM1);  // Text
  201.       Text(Wnd->RPort,(UBYTE*)"Wait! Remapping graphics..",26); //
  202.  
  203.       if(cm=Scr->ViewPort.ColorMap)
  204.       {
  205.         // Allocate colors and remap graphics
  206.         colors = 1<<Cards.Depth;
  207.  
  208.         for(int i=0; i<colors ; i++ ) {
  209.           gfxpens[i] = ObtainBestPen(cm,
  210.                                 (ULONG)ColorTable[3*i]*0x1010101,
  211.                                 (ULONG)ColorTable[3*i+1]*0x1010101,
  212.                                 (ULONG)ColorTable[3*i+2]*0x1010101,
  213.                                 0x84000000,0xFFFFFFFF,
  214.                                 TAG_DONE);
  215.         }
  216.         ReMapBitMap(Cards,&Scr->RastPort,gfxpens);
  217.         pens[0] = ObtainBestPen(cm,0xACACACAC,
  218.                                    0xACACACAC,
  219.                                    0xACACACAC,
  220.                                    0x84000000,0xFFFFFFFF,
  221.                                    TAG_DONE);
  222.         pens[1] = ObtainBestPen(cm,0,0,0,
  223.                                    0x84000000,0xFFFFFFFF,
  224.                                    TAG_DONE);
  225.       }
  226.  
  227.       SetABPenDrMd(Wnd->RPort,gfxpens[0],gfxpens[0],JAM1);
  228.       RectFill(Wnd->RPort,Wnd->BorderLeft,Wnd->BorderTop,
  229.                           Wnd->Width-Wnd->BorderRight,
  230.                           Wnd->Height-Wnd->BorderBottom);
  231.  
  232.       Move(Wnd->RPort,150+Wnd->BorderLeft,23+Wnd->BorderTop);    // Print Double
  233.       SetAPen(Wnd->RPort,pens[1]); SetDrMd(Wnd->RPort,JAM1);  // Text
  234.       Text(Wnd->RPort,(UBYTE*)"Money:",6); //
  235.  
  236.       RefreshWindowFrame(Wnd);
  237.  
  238.       {
  239.         int terminated=FALSE;
  240.         cGamePoker gamepoker(Wnd,&Cards,prefsjoker,prefs5kind,changes,jokers);
  241.         cPokerMenu menu(Wnd,&gamepoker);
  242.         cPokerGadgets pokergads(Wnd,&gamepoker);
  243.         cIMGCard *const*cards=gamepoker.Cards();
  244.  
  245.         while(!terminated) {
  246.           Message *msg;
  247.  
  248.           WaitPort(Wnd->UserPort);
  249.           while(msg=GetMsg(Wnd->UserPort)) {
  250.  
  251.             if((RT=menu.IDCMP((const IntuiMessage *)msg))==IDCMP_NOT_DONE)
  252.               if((RT=pokergads.IDCMP((const IntuiMessage *)msg))==IDCMP_NOT_DONE)
  253.                 if((RT=IDCMP((const IntuiMessage *)msg,cards))==IDCMP_NOT_DONE)
  254.                   switch(((IntuiMessage *)msg)->Class) {
  255.                     case IDCMP_CLOSEWINDOW:
  256.                         {
  257.                           EasyStruct myES = {
  258.                             sizeof(struct EasyStruct),0,
  259.                             (UBYTE*)"Quit Poker",
  260.                             (UBYTE*)"Are You Sure to Quit?",
  261.                             (UBYTE*)"Quit|Cancel"
  262.                           };
  263.                           switch(EasyRequestArgs(Wnd, &myES, NULL, NULL)) {
  264.                             case 1: terminated=TRUE;
  265.                             case 0: break;
  266.                           }
  267.                         }
  268.                     break;
  269.                   }
  270.             if(RT==IDCMP_TERMINATED) terminated=TRUE;
  271.             if(RT!=IDCMP_REPLYED) ReplyMsg(msg);
  272.           }
  273.         }
  274.  
  275.         prefsjoker=gamepoker.PrefsJoker();
  276.         prefs5kind=gamepoker.Prefs5Kind();
  277.         changes=gamepoker.Changes();
  278.         jokers=gamepoker.Jokers();
  279.  
  280.         // Save preferences to Tool Types
  281.         {
  282.           struct DiskObject *dobj;
  283.           char **oldtooltypes;
  284.           LONG odir=-1;
  285.           WBArg *wbarg=_WBenchMsg->sm_ArgList;
  286.           if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  287.             odir=CurrentDir(wbarg->wa_Lock);
  288.  
  289.  
  290.           if((*wbarg->wa_Name) && (dobj=GetDiskObject((UBYTE*)wbarg->wa_Name)))
  291.           {
  292.             char *newtooltypes[]={
  293.               { "PREFSJOKER=FALSE"},
  294.               { "PREFS5KIND=FALSE"},
  295.               { "CHANGES=0"},
  296.               { "JOKERS=0"},
  297.               NULL,
  298.               NULL
  299.             };
  300.             cstring tooltype="CARDS=";
  301.             tooltype+=CardFile;
  302.  
  303.             if(prefsjoker) newtooltypes[0]="PREFSJOKER=TRUE";
  304.             if(prefs5kind) newtooltypes[1]="PREFS5KIND=TRUE";
  305.             newtooltypes[2][8]+=changes;
  306.             newtooltypes[3][7]+=jokers;
  307.             newtooltypes[4]=(char*)tooltype.chars();
  308.  
  309.             oldtooltypes = dobj->do_ToolTypes;
  310.  
  311.             dobj->do_ToolTypes = newtooltypes;
  312.  
  313.             PutDiskObject((UBYTE*)wbarg->wa_Name,dobj);
  314.  
  315.             dobj->do_ToolTypes = oldtooltypes;
  316.  
  317.             FreeDiskObject(dobj);
  318.           }
  319.           if(odir != -1) CurrentDir(odir); /* CD back where we were */
  320.         }
  321.  
  322.         CloseWindow(Wnd);
  323.         RT=0;
  324.       }
  325.     }
  326.   }
  327.  
  328. cleanup:
  329.     if(olddir != -1) CurrentDir(olddir); /* CD back where we were */
  330.  
  331.   if(Scr) {
  332.     if (cm) {
  333.       for (int i = 0; i < colors ; i++) ReleasePen(cm,gfxpens[i]);
  334.       ReleasePen(cm,pens[0]);
  335.       ReleasePen(cm,pens[1]);
  336.     }
  337.     UnlockPubScreen(PubScreenName,Scr);
  338.   }
  339.  
  340.   FreeBlocks(Cards);
  341.  
  342.   if(TEXTFONT) CloseFont(TEXTFONT);
  343.   return RT;
  344. }
  345.